/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import domain.controller.file.CategoriaController;
import domain.model.file.Categoria;
import java.util.Enumeration;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
import util.J2meAppHelper;
import view.components.MObjectList;

/**
 *
 * @author Victorino
 */
public class CategoriaForm implements CommandListener, AppForm {

    private MObjectList list = new MObjectList();
    private Categoria catCurrent;
    private Command cmdSelect = new Command("Selecionar", Command.ITEM, 1);
    private Command cmdBegin = new Command("Início", Command.ITEM, 2);

    public CategoriaForm() {
        list.addCommand(cmdSelect);
        list.addCommand(cmdBegin);
        list.setCommandListener(this);
    }

    public void commandAction(Command arg0, Displayable arg1) {

        try {
            if (arg0.equals(cmdBegin)) {
                J2meAppHelper.getInstance().changeForm(new CategoriaRaizForm(), null);
                return;
            }

            if (list.isBackCmdClick(arg0)) { // voltar
                if (catCurrent.getId() == -1 || catCurrent.getCategoriaPaiId() == -1) { // quando nao tem pai
                    J2meAppHelper.getInstance().changeForm(new CategoriaRaizForm());
                } else { // volta para a pai
                    Categoria catAvo = (Categoria) CategoriaController.getInstance().getCategoria(catCurrent.getCategoriaPaiId());
                    J2meAppHelper.getInstance().changeForm(new CategoriaForm(), catAvo);
                }
                return;
            }

            Categoria catSelected = (Categoria) list.getSelectedMObject();

            if (catSelected.getRegistrosIds().size() > 0) { // exibir registros
                J2meAppHelper.getInstance().changeForm(new RegistroForm(), catSelected, true);
            } else if (catSelected.getCategoriaPaiId() == -1) { // retorna para o pai
                J2meAppHelper.getInstance().changeForm(new CategoriaRaizForm(), new Integer(catCurrent.getId()));
            } else { // exibe filha
                J2meAppHelper.getInstance().changeForm(new CategoriaForm(), catSelected, true);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Displayable getDisplyable() {
        return list;
    }

    public void initialize(Object parameter) {
        try {
            if (!(parameter instanceof Categoria)) {
                return;
            }
            catCurrent = (Categoria) parameter;
            if(catCurrent.getDescricao() != null && !"".equals(catCurrent.getDescricao())){
                list.setTitle(catCurrent.getDescricao());
            }
            list.deleteAll();
            Enumeration catsIds = CategoriaController.getInstance().listSubCategoria(catCurrent).elements();
            while (catsIds.hasMoreElements()) {
                Categoria categoria = (Categoria) catsIds.nextElement();
                list.appendMObject(categoria);
            }
            list.sortByLabelAlpha();
            if (catCurrent.getCategoriaPaiId() == -1) {
                list.appendBack(new CategoriaRaizForm());
            } else {
                //TODO: passando o pai
                list.appendBack(new CategoriaForm(), catCurrent);
            }
            System.gc();
            list.setTicker(new Ticker(catCurrent.getNome()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
